package com.ddwl.goods.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.ShopApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.goods.dao.GoodsProfitDao;
import com.ddwl.goods.dao.ItemDao;
import com.ddwl.goods.dao.model.GoodsProfit;
import com.ddwl.goods.model.Item;
import com.ddwl.schema.bo.distr.ComputeProfitPriceBo;
import com.ddwl.schema.bo.distr.SetGoodsProfitBo;
import com.ddwl.schema.bo.goods.QueryGoodsProfitBo;
import com.ddwl.schema.vo.dist.ComputeProfitPriceVo;
import com.ddwl.schema.vo.dist.GoodsProfitVo;
import com.ddwl.schema.vo.shop.ShopVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类GoodsProfitService.java的实现描述：商品分润配置
 *
 * @author zach
 * @Date :2020-05-25 14:19.
 */
@Service
@RequiredArgsConstructor
public class GoodsProfitService {

    private final GoodsProfitDao goodsProfitDao;
    private final CategoryService categoryService;
    private final ShopApi shopApi;
    private final ItemDao itemDao;

    /**
     * 分润设置
     *
     * @param goodsProfitVo
     */
    public void setGoodsProfit(SetGoodsProfitBo goodsProfitVo) {
        ShopVo shopVo = shopApi.getDefaultShopForApi().getResult();
        goodsProfitVo.setShopId(shopVo.getId());
        verfiyGoodsProfit(goodsProfitVo);
        GoodsProfit goodsProfit = goodsProfitDao.getGoodsProfit(goodsProfitVo.getTargetId(), goodsProfitVo.getTargetType());
        if (goodsProfit != null) {
            BeanUtil.copyProperties(goodsProfitVo, goodsProfit);
            goodsProfitDao.updateByPrimaryKeySelective(goodsProfit);
        } else {
            goodsProfit = BeanConverUtils.convertBean(goodsProfitVo, GoodsProfit.class);
            goodsProfitDao.insert(goodsProfit);
        }
    }

    /**
     * 设置商品分润信息
     */
    public void setItemProfit(SetGoodsProfitBo bo, String itemId) {
        if (bo != null) {
            bo.setTargetId(itemId);
            bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.SPU.getCode());
            setGoodsProfit(bo);
        } else {
            GoodsProfitVo vo = queryGoodsProfitVoByItemId(itemId);
            if (vo != null) {
                deleteGoodsProfit(vo.getId());
            }
        }
    }

    /**
     * 删除分润配置
     *
     * @param id
     */
    public void deleteGoodsProfit(String id) {
        ParamException.isBlank(id);
        GoodsProfit goodsProfit = goodsProfitDao.selectByPrimaryKey(id);
        ParamException.isNull(goodsProfit);
        GoodsProfit goodsProfit1 = new GoodsProfit();
        goodsProfit1.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        goodsProfit1.setId(id);
        goodsProfitDao.updateByPrimaryKeySelective(goodsProfit1);
    }

    /**
     * 查询商品分润信息
     *
     * @param itemIds
     * @return
     */
    public List<GoodsProfitVo> queryGoodsProfitVoByItemIds(List<String> itemIds) {
        QueryGoodsProfitBo bo = new QueryGoodsProfitBo();
        bo.setTargetIds(StrUtil.join(Constant.DEFAULT_SEPARATOR, itemIds));
        bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.SPU.getCode());
        return queryGoodsProfitVo(bo);
    }


    /**
     * 查询类目分润信息
     *
     * @param categoryIds
     * @return
     */
    public List<GoodsProfitVo> queryGoodsProfitVoByCategoryIds(List<String> categoryIds) {
        QueryGoodsProfitBo bo = new QueryGoodsProfitBo();
        bo.setTargetIds(StrUtil.join(Constant.DEFAULT_SEPARATOR, categoryIds));
        bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.CATEGORY.getCode());
        return queryGoodsProfitVo(bo);
    }

    /**
     * 查询商品分润信息
     *
     * @param itemId
     * @return
     */
    public GoodsProfitVo queryGoodsProfitVoByItemId(String itemId) {
        QueryGoodsProfitBo bo = new QueryGoodsProfitBo();
        bo.setTargetId(itemId);
        bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.SPU.getCode());
        List<GoodsProfitVo> goodsProfits = queryGoodsProfitVo(bo);
        if (!CollectionUtils.isEmpty(goodsProfits)) {
            return goodsProfits.get(0);
        }
        return null;
    }


    /**
     * 计算商品分润金额
     */
    public List<ComputeProfitPriceVo> computeProfitPrice(List<ComputeProfitPriceBo> itemBos) {
        List<ComputeProfitPriceBo> list = new ArrayList<>();
        //去除打折和限时购的分润
        List<Item> itemListByIds = itemDao.findItemListByIds(itemBos.stream().map(ComputeProfitPriceBo::getItemId).collect(Collectors.toList()));
        for (Item item : itemListByIds) {
            for (ComputeProfitPriceBo computeProfitPriceBo : itemBos) {
                if (item.getId().equals(computeProfitPriceBo.getItemId())
                        && (!BaseEnum.StatusEnum.ENABLE.getCode().equals(item.getLimitTimeFlag()) && !BaseEnum.StatusEnum.ENABLE.getCode().equals(item.getDiscountFlag()))) {
                    list.add(computeProfitPriceBo);
                }
            }
        }

        List<ComputeProfitPriceVo> itemVos = BeanConverUtils.convertBeanList(list, ComputeProfitPriceVo.class);
        //获取商品分润
        List<GoodsProfitVo> itemProfitVos = queryGoodsProfitVoByItemIds(itemVos.stream().map(ComputeProfitPriceVo::getItemId).collect(Collectors.toList()));
        //获取分类优惠信息
        List<GoodsProfitVo> categoryProfitVos = queryGoodsProfitVoByCategoryIds(itemVos.stream().map(ComputeProfitPriceVo::getCategoryId).collect(Collectors.toList()));
        for (ComputeProfitPriceVo itemVo : itemVos) {
            BigDecimal itemPrice = BigDecimal.ZERO;
            BigDecimal categoryPrice = BigDecimal.ZERO;
            //获取商品分润
            for (GoodsProfitVo itemProVo : itemProfitVos) {
                if (BaseEnum.StatusEnum.ENABLE.getCode().equals(itemProVo.getStatus()) && itemProVo.getTargetId().equals(itemVo.getItemId())) {
                    itemPrice = itemProVo.getProfitPrice(itemVo.getPrice());
                    break;
                }
            }
            for (GoodsProfitVo itemProVo : categoryProfitVos) {
                if (BaseEnum.StatusEnum.ENABLE.getCode().equals(itemProVo.getStatus()) && itemProVo.getTargetId().equals(itemVo.getCategoryId())) {
                    categoryPrice = itemProVo.getProfitPrice(itemVo.getPrice());
                    break;
                }
            }
            if (itemPrice.compareTo(BigDecimal.ZERO) > 0) {
                itemVo.setProfitPrice(itemPrice);
            } else if (categoryPrice.compareTo(BigDecimal.ZERO) > 0) {
                itemVo.setProfitPrice(categoryPrice);
            } else {
                itemVo.setProfitPrice(BigDecimal.ZERO);
            }
        }
        return itemVos;
    }

    /**
     * 计算商品分润金额
     */
    public ComputeProfitPriceVo computeProfitPrice(ComputeProfitPriceBo itemBo) {
        ComputeProfitPriceVo itemVo = BeanConverUtils.convertBean(itemBo, ComputeProfitPriceVo.class);
        //获取商品分润
        GoodsProfitVo itemProfitVo = BeanConverUtils.convertBean(goodsProfitDao.getGoodsProfit(itemBo.getItemId(), GoodsEnum.ProfitTagetTypeEnum.SPU.getCode()), GoodsProfitVo.class);
        GoodsProfitVo categoryProfit = BeanConverUtils.convertBean(goodsProfitDao.getGoodsProfit(itemBo.getCategoryId(), GoodsEnum.ProfitTagetTypeEnum.CATEGORY.getCode()), GoodsProfitVo.class);
        if (itemProfitVo != null && BaseEnum.StatusEnum.ENABLE.getCode().equals(itemProfitVo.getStatus())) {
            itemVo.setProfitPrice(itemProfitVo.getProfitPrice(itemVo.getPrice()));
        } else if (categoryProfit != null && BaseEnum.StatusEnum.ENABLE.getCode().equals(categoryProfit.getStatus())) {
            itemVo.setProfitPrice(categoryProfit.getProfitPrice(itemVo.getPrice()));
        }
        return itemVo;
    }


    /**
     * 查询分润信息
     *
     * @param queryGoodsProfitVo
     * @return
     */
    public List<GoodsProfitVo> queryGoodsProfitVo(QueryGoodsProfitBo queryGoodsProfitVo) {
        return BeanConverUtils.convertBeanList(goodsProfitDao.queryGoodsProfits(queryGoodsProfitVo), GoodsProfitVo.class);
    }


    private void verfiyGoodsProfit(SetGoodsProfitBo goodsProfitVo) {
        ParamException.isNull(GoodsEnum.ProfitTagetTypeEnum.tagetTypeEnum(goodsProfitVo.getTargetType()));
        if (GoodsEnum.ProfitTagetTypeEnum.SPU.getCode().equals(goodsProfitVo.getTargetType())) {
            ParamException.isNull(itemDao.selectByPrimaryKey(goodsProfitVo.getTargetId()));
        } else if (GoodsEnum.ProfitTagetTypeEnum.CATEGORY.getCode().equals(goodsProfitVo.getTargetType())) {
            ParamException.isNull(categoryService.detail(goodsProfitVo.getTargetId()));
        } else if (GoodsEnum.ProfitTagetTypeEnum.PLATFORM.getCode().equals(goodsProfitVo.getTargetType())) {

        }
        ParamException.isNull(GoodsEnum.ProfitTypeEnum.tagetTypeEnum(goodsProfitVo.getProfitType()));
        if (GoodsEnum.ProfitTypeEnum.RATIO.getCode().equals(goodsProfitVo.getProfitType())) {
            ParamException.isFalse(goodsProfitVo.getRatio().compareTo(new BigDecimal("0.00")) >= 0 && goodsProfitVo.getRatio().compareTo(new BigDecimal("99.99")) <= 0, "佣金百分比不正确");
        } else {
            ParamException.isFalse(goodsProfitVo.getRatio().compareTo(new BigDecimal("0.00")) >= 0);
        }
    }


}
